package com.example.xxpk.service.outifts;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xxpk.mapper.OutiftsMapper;
import com.example.xxpk.mapper.UserOutiftsMapper;
import com.example.xxpk.mapper.UsersMapper;
import com.example.xxpk.pojo.Outifts;
import com.example.xxpk.pojo.UserOutifts;
import com.example.xxpk.pojo.Users;
import com.example.xxpk.service.purchaserecord.IPurchaseRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangguihong
 * @since 2025-02-12
 */
@Service
@RequiredArgsConstructor
public class OutiftsServiceImpl extends ServiceImpl<OutiftsMapper, Outifts> implements IOutiftsService {

    private final UsersMapper usersMapper;

    private final OutiftsMapper outiftsMapper;

    private final UserOutiftsMapper userOutiftsMapper;

    private final IPurchaseRecordService purchaseRecordService;
    @Override
    public boolean purchaseOutfit(Integer userId, Integer outiftsId) {
        // 查询用户信息
        Users user = usersMapper.selectById(userId);
        if (user == null) {
            throw new RuntimeException("未查询到用户信息");
        }

        // 查询装扮信息
        Outifts outfit = outiftsMapper.selectById(outiftsId);
        if (outfit == null) {
            throw new RuntimeException("未查询到装扮信息");
        }

        QueryWrapper<UserOutifts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("outifts_id", outiftsId);
        UserOutifts userOutifts1 = userOutiftsMapper.selectOne(queryWrapper);
        if(userOutifts1 != null ){
            if ("1".equals(userOutifts1.getStatus())) {
                throw new RuntimeException("你已拥有此装扮");
            }
        }

        // 判断用户是否有足够的资源购买装扮
        if (user.getPremiumDiamond() >= outfit.getPrice() && user.getUniversalProps() >= outfit.getPropsNum()) {
            // 扣除用户的资源
            int rows = usersMapper.updateUserResources(userId, outfit.getPrice(), outfit.getPropsNum());
            if (rows > 0) { //新增用户装扮记录
                UserOutifts userOutifts = new UserOutifts();
                userOutifts.setUserId(userId);
                userOutifts.setOutiftsId(outiftsId);
                userOutifts.setCategory(outfit.getCategory());
                Date now = new Date();
                Instant instant = now.toInstant();
                LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
                userOutifts.setAcquiredTime(localDateTime);
                userOutifts.setLevel(1);
                userOutifts.setExperience(0);
                userOutifts.setStatus(1); // 已拥有
                userOutifts.setIsUsing(0);
                userOutiftsMapper.insert(userOutifts);
                return true;
            }
        }
        return false;
    }
}
